const mysql = require("mysql");

//创建连接池
const pool = mysql.createPool({
  host: "124.221.62.120",
  user: "root",
  password: "helloworld",
  database: "my_db_01",
  port: 3306,
});

//使用Promise封装query方法
const query = (sql, values) => {
  return new Promise((resolve, reject) => {
    pool.getConnection((err, connection) => {
      if (err) {
        reject(err);
      } else {
        connection.query(sql, values, (err, rows) => {
          if (err) {
            reject(err);
          } else {
            resolve(rows);
          }
          connection.release();
        });
      }
    });
  });
};

//向外导出
module.exports = query;
